package ar.com.fiuba.ElectronicCircuitAnalyst.domain.impl;

import java.util.ArrayList;
import java.util.List;

import ar.com.fiuba.ElectronicCircuitAnalyst.application.api.ComponentDto;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.Component;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.ComponentFactory;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.Lead;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.LogicalFunction;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.Output;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.common.Specification;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.exceptions.InvalidCircuitElementException;

public class ComponentFactoryImpl implements ComponentFactory {

	private Specification<Component> specification;

	public ComponentFactoryImpl(Specification<Component> specification) {
		this.specification = specification;
	}

	public Component makeComponent(ComponentDto data) throws InvalidCircuitElementException {
		String name = data.getName();
		String type = data.getType();
		List<Lead> inputLeads = new ArrayList<Lead>();
		List<Output> outputs = new ArrayList<Output>();
		List<LogicalFunction> functions = new ArrayList<LogicalFunction>();

		for (int i = 0; i < data.getInputsCounts(); i++)
			inputLeads.add(new LeadImpl());

		for (int i = 0; i < data.getFunctions().size(); i++) {
			LogicalFunctionImpl function = new LogicalFunctionImpl(data.getFunctions().get(i));
			functions.add(function);
			outputs.add(new OutputImpl(new LeadImpl(), function));
		}

		return new ComponentImpl(name, type, inputLeads, outputs, functions, specification);
	}

}
